package org.anderground.editor.gui.properties.tunnel;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import org.anderground.core.model.Tunnel;
import org.anderground.editor.edits.EditsManager;
import org.anderground.editor.edits.SetTunnelSmoothAction;
import org.anderground.editor.gui.properties.AbstractPropertiesPanel;

public class TunnelSmoothProperty implements AbstractPropertiesPanel.Property {

	@Override
	public TableCellEditor getCellEditor() {

		@SuppressWarnings("serial")
		class Editor extends AbstractCellEditor implements TableCellEditor {

			@Override
			public Component getTableCellEditorComponent(JTable table,
					final Object value, boolean isSelected, final int row,
					int column) {
				final JCheckBox box = new JCheckBox();
				final Tunnel tunnel = (Tunnel) value;
				box.setSelected(tunnel.isSmooth());
				box.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						Tunnel tunnel = (Tunnel) value;
						EditsManager.getInstance().performAction(
								new SetTunnelSmoothAction(tunnel, box
										.isSelected()));
						Editor.this.cancelCellEditing();
					}

				});
				return box;
			}

			@Override
			public Object getCellEditorValue() {
				return null;
			}

		}

		return new Editor();
	}

	@Override
	public TableCellRenderer getCellRender() {
		class Render implements TableCellRenderer {

			@Override
			public Component getTableCellRendererComponent(JTable arg0,
					Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
				final JCheckBox box = new JCheckBox();
				box.setSelected(((Tunnel) arg1).isSmooth());
				return box;
			}

		}
		return new Render();
	}

	@Override
	public String getPropertyName() {
		return "Smooth";
	}

	@Override
	public boolean isEditable() {
		return true;
	}

}
